<template>
  <epm-pagination :total-count="total" @query="pageQuery" ref="pager"/>
</template>

<script>
import EpmPagination from "./EpmPagination";
import Request from "@/utils/Request";

export default {
  name: "SimplePager",
  components: {EpmPagination},
  props: {
    defaultParam: {
      type: Object,
      default: () => ({})
    },
    requestUrl: {
      type: String,
      default: '',
      required: true
    },
    tableData: {
      type: Array,
      default: () => [],
      required: true
    },
    errorMessage: {
      type: String,
      default: '查询失败'
    },
    requestType: {
      type: String,
      default: 'get'
    }
  },
  data() {
    return {
      total: 0,
      queryParam: {}
    }
  },
  mounted() {

  },
  methods: {
    init() {
      if (this.requestType === 'get') {
        Request.get(`${this.requestUrl}`, {params: {...this.defaultParam, ...this.queryParam}})
            .then(({data: {success, data: {rows, total}}}) => {
              if (success) {
                this.total = total;
                this.$emit('update:tableData', rows);
                this.$emit('success', rows);
              } else {
                this.$message({type: 'error', message: this.errorMessage});
              }
            });
      } else {
        Request.post(`${this.requestUrl}`, {...this.defaultParam, ...this.queryParam})
            .then(({data: {success, data: {rows, total}}}) => {
              if (success) {
                this.total = total;
                this.$emit('update:tableData', rows);
              } else {
                this.$message({type: 'error', message: this.errorMessage});
              }
            });
      }

    },
    //仅本组件调用
    pageQuery(param) {
      this.queryParam = this.$refs.pager.wrapQueryModel(this.queryParam, param);
      this.init();
    },
    //仅父组件条用
    doQuery(param) {
      this.queryParam = this.$refs.pager.wrapQueryModel(param);
      this.init();
    }
  }
}
</script>

<style scoped>

</style>